﻿using System;
using System.Collections.Generic;
using System.Threading;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Threading;
using ControleRepresentanteSync.Application;
using Cursors = System.Windows.Input.Cursors;
using MessageBox = System.Windows.MessageBox;

namespace ControleRepresentanteSync.View
{
    public class UserInterfaceService : IUserInterfaceService
    {
        private readonly Dispatcher m_Dispatcher;

        public UserInterfaceService(Dispatcher dispatcher)
        {
            m_Dispatcher = dispatcher;
        }

        public MessageBoxResult ShowMessage(string message, string caption, MessageBoxButton button, MessageBoxImage image)
        {
            return MessageBox.Show(message, caption, button, image);
        }

        public void InvokeOnUIThread(Action action)
        {
            if (m_Dispatcher.Thread == Thread.CurrentThread)
            {
                action();
            }
            else
            {
                m_Dispatcher.Invoke(DispatcherPriority.Normal, action);
            }
        }

        public void Exit(int exitCode)
        {
            Environment.Exit(exitCode);
        }

        public void ShowMessage(string message)
        {
            MessageBox.Show(message);
        }

        public bool OpenWindow(IWindow window)
        {
            return window.ShowDialog() == true;
        }

        public IDisposable GetDisposableWaitCursor()
        {
            return new DisposableWaitCursor();
        }

        public void ExecuteActionWithWaitMessage(string message, IWindow owner, Action action)
        {
            Mouse.SetCursor(Cursors.Wait);
            MessageWindow messageWindow = new MessageWindow(message);
            Thread thread = new Thread(() =>
            {
                try
                {
                    action();
                }
                finally
                {
                    InvokeOnUIThread(messageWindow.Close);
                    Mouse.SetCursor(Cursors.Arrow);
                }
            });
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
            messageWindow.Owner = (Window) owner;
            messageWindow.ShowDialog();
        }

        public string SelectFolder(string description, bool isShowNewFolderButton = false)
        {
            FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog
            {
                ShowNewFolderButton = isShowNewFolderButton,
                Description = description
            };
            return folderBrowserDialog.ShowDialog() == DialogResult.OK ? folderBrowserDialog.SelectedPath : null;
        }

        public IEnumerable<string> SelectFiles(string description)
        {
            FileDialog fileDialog = new OpenFileDialog
            {
                Title = description,
                Multiselect = true
            };
            return fileDialog.ShowDialog() == DialogResult.OK ? fileDialog.FileNames : null;
        }

        private class DisposableWaitCursor : IDisposable
        {
            public DisposableWaitCursor()
            {
                Mouse.SetCursor(Cursors.Wait);
            }

            public void Dispose()
            {
                Mouse.SetCursor(Cursors.Arrow);
            }
        }
    }
}